rm(list = ls())
load("dataBJPOLS.RData")

tests2 <- function(name, x1, y1) {
    t1 <- t.test(x1[y1 == 1], x1[y1 == 0], alternative = "two.sided", var.equal = FALSE)

    x <- data.table(round(c(t1$estimate[1] - t1$estimate[2], t1$p.value), 2))
    x$V4 <- c(paste("difference in", name), "p-value")
    colnames(x) <- c("Experienced-Inexperienced",
                     "Variable")
    return(x)
}

t1 <- rbind(tests2(name = "PTI", x1 = last.cases$pti, y1 = last.cases$judge_exp_cat),
            tests2(name = "Age", x1 = last.cases$age, y1 = last.cases$judge_exp_cat),
            tests2(name = "Black defendant", x1 = last.cases$race4 == "B", y1 = last.cases$judge_exp_cat),
            tests2(name = "White defendant", x1 = last.cases$race4 == "W", y1 = last.cases$judge_exp_cat),
            tests2(name = "Hispa defendant", x1 = last.cases$race4 == "H", y1 = last.cases$judge_exp_cat),
            tests2(name = "Female", x1 = last.cases$female, y1 = last.cases$judge_exp_cat),
            tests2(name = "Prev. Turnout", x1 = last.cases$vote_pre, y1 = last.cases$judge_exp_cat),
            tests2(name = "Turnout", x1 = last.cases$vote_post, y1 = last.cases$judge_exp_cat),
            tests2(name = "Not Eligible", x1 = last.cases$noteli, y1 = last.cases$judge_exp_cat),
            tests2(name = "Registration", x1 = last.cases$regis_before, y1 = last.cases$judge_exp_cat),
            tests2(name = "Severity", x1 = last.cases$severity, y1 = last.cases$judge_exp_cat),            
            tests2(name = "Any Drug", x1 = last.cases$any_drug, y1 = last.cases$judge_exp_cat),
            tests2(name = "Any Weapon", x1 = last.cases$any_weapon, y1 = last.cases$judge_exp_cat),
            tests2(name = "Any Property", x1 = last.cases$any_prop, y1 = last.cases$judge_exp_cat),
            tests2(name = "Any Prior", x1 = last.cases$any_prior_case, y1 = last.cases$judge_exp_cat),
            tests2(name = "Number of Days", x1 = last.cases$daysjail_t, y1 = last.cases$judge_exp_cat))
t1

tests3 <- function(name, x1, y1, y2) {
    t1 <- t.test(x1[y1 == 1 & y2 == "B"], x1[y1 == 0 & y2 == "B"], alternative = "two.sided", var.equal = FALSE)

    x <- data.table(round(c(t1$estimate[1] - t1$estimate[2], t1$p.value), 2))
    x$V4 <- c(paste("difference in", name), "p-value")
    colnames(x) <- c("Experienced-Inexperienced Judge (Black Defendants)",
                     "Variable")
    return(x)
}

t2 <- rbind(tests3(name = "PTI", x1 = last.cases$pti, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Age", x1 = last.cases$age, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Female", x1 = last.cases$female, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Prev. Turnout", x1 = last.cases$vote_pre, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Turnout", x1 = last.cases$vote_post, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Not Eligible", x1 = last.cases$noteli, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Registration", x1 = last.cases$regis_before, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Severity", x1 = last.cases$severity, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),            
            tests3(name = "Any Drug", x1 = last.cases$any_drug, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Any Weapon", x1 = last.cases$any_weapon, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Any Property", x1 = last.cases$any_prop, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Any Prior", x1 = last.cases$any_prior_case, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4),
            tests3(name = "Number of Days", x1 = last.cases$daysjail_t, y1 = last.cases$judge_exp_cat, y2 = last.cases$race4))
t2
